<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Hello World</title>
    <style>
        table{
            margin: 0 auto;
            position: relative;
            top: 100px;
            border: 1px solid;
            width: 420px;
            height: 250px;
        }
        .main1{
            position: absolute;
            top: 100px;
            left: 100px;
        }
        .top{
            position: absolute;
            top: 25px;
            left: 120px;
        }
        .check{
            position: relative;
            top: 100px;
            left: 120px;
        }

    </style>
</head>
<body>
<div name="">

    <%
        //判断cookie中是否有username，如果有用户名，直接跳转
        Cookie[] cookies = request.getCookies();
        if(cookies!=null) {
            for (int i = 0; i < cookies.length; i++) {
                System.out.println("cookies[" + i + "].getName()" + cookies[i].getName());

                if (cookies[i].getName().equals("username")) {
                    request.getRequestDispatcher("/show").forward(request, response);
                }
            }
        }
    %>
    <table >
        <tr>
            <td>
                <form action="/test?cookie=no" onsubmit="return validateForm()" method="post" id="from">
                    <div class="top">
                        AAA教育注册页面
                    </div>
                    <div class="main1">
                        用户名:<input type="text" name="username" id="username"><br><br>
                        密码：<input type="password" name="password" id="password"><br><br>
                        <div class="check"><input type="checkbox" id="myCheckbox">两周之内免登录</div>
                        <button type="submit">提交数据</button>
                    </div>
                </form>
            </td>
        </tr>





        <script>
            const checkbox = document.getElementById("myCheckbox");
            checkbox.addEventListener("click", function() {
                if (this.checked) {
                    const form = document.getElementById("from"); // 获取表单元素
                    form.action = "/test?cookie=yes";
                } else {
                    const form = document.getElementById("from"); // 获取表单元素
                    form.action = "/test?cookie=no";
                }
            });

            function validateForm() {
                var username = document.getElementById("username").value;
                var password = document.getElementById("password").value;

                if (username == ""|| password=="") {
                    alert("账号或密码错误");
                    return false;
                }
            }


        </script>
    </table>
</div>
</body>